package com.welse.modulebiz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welse.modulebiz.domain.ApArticle;
import com.welse.modulebiz.domain.ArticleDto;
import com.welse.modulebiz.domain.Result;
import com.welse.modulebiz.service.ApArticleService;
import com.welse.modulebiz.mapper.ApArticleMapper;
import com.welse.modulebiz.utils.DateUtil;
import org.apache.catalina.util.Introspection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 54036
* @description 针对表【ap_article(文章信息表，存储已发布的文章)】的数据库操作Service实现
* @createDate 2025-08-14 09:24:36
 *@Param dto
 *@Param  type 1 加载更多  2 刷新
*/
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle>
    implements ApArticleService{

    @Autowired
    private ApArticleMapper apArticleMapper;

    public static final int DEFAULT_SIZE = 10;
    public static final short MAX_SIZE = 50;

    @Override
    public Result<List<ApArticle>> loadArticleList(ArticleDto dto, short type) {

        //1.校验参数
        //分页条数的校验
        Integer size = dto.getSize();
        if(size == null || size == 0){
            size = DEFAULT_SIZE;
        }
        //分页的值不超过50
        size = Math.min(size, MAX_SIZE);
        // 确保dto中的size已经被设置，避免Mapper中出现null值
        dto.setSize(size);

        //参数校验  --> type
        if(type != 1 && type != 2){
            type = 1;
        }

        //频道参数校验  --> Tag
        String tag = dto.getTag();
        if(StringUtils.isEmpty(tag)){
            tag = "__all__";
        }

        //时间参数校验
        if(dto.getMaxBehotTime()== null) dto.setMaxBehotTime(DateUtil.getCurrentDate());//获取当前时间
        if(dto.getMinBehotTime()== null) dto.setMinBehotTime(DateUtil.getBeforeYears(100));

        List<ApArticle> articleList = apArticleMapper.loadArticleList(dto, type);
        return Result.success(articleList);
    }
}




